#include "RangeListXml.h"

RangeListXml::RangeListXml()
{
    m_rangePointList.clear();
}
QList<RangePointStruct> RangeListXml::rangePointList() const
{
    return m_rangePointList;
}


bool RangeListXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "ResolvePT")   //解析场景点位
        {


            RangePointStruct rangePoint;
            rangePoint.setAddress(rootChild.toElement().attribute("Address").toInt());
            QStringList strTemp = rootChild.toElement().attribute("Value").split("-");
            int valueMin = strTemp.at(0).toInt();
            int valueMax = strTemp.at(1).toInt();
            rangePoint.setValueMin(valueMin);
            rangePoint.setValueMax(valueMax);

            QList<ControlPointStruct> controlPointList;
            QDomNode resolvePTChild = rootChild.toElement().firstChild();
            while(!resolvePTChild.isNull())
            {
                if (resolvePTChild.toElement().tagName() == "ControlPT")  //解析控制点位
                {
                    ControlPointStruct controlPoint;
                    strTemp = resolvePTChild.toElement().text().split(",");
                    controlPoint.setAddress(strTemp.at(0).toInt());
                    controlPoint.setValue(strTemp.at(1).toInt());
                    controlPointList.append(controlPoint);
                }
                resolvePTChild = resolvePTChild.nextSibling();
            }
            rangePoint.setControlPointList(controlPointList);
             m_rangePointList.append(rangePoint);

        }
        rootChild = rootChild.nextSibling();
    }
    return true;
}

bool RangeListXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_rangePointList.clear();
    return true;
}

QList<ControlPointStruct> RangeListXml::controlPointList(const ControlPointStruct &controlPoint)
{
    quint16 address = controlPoint.address();
    quint16 value   = controlPoint.value();
    QList<ControlPointStruct> controlPointList;
    for(int i=0; i<m_rangePointList.size(); i++)
    {
        RangePointStruct rangePoint = m_rangePointList.at(i);
        if(address == rangePoint.address())
        {
            if((value >= rangePoint.valueMin()) && (value <= rangePoint.valueMax()))
            {
                controlPointList =  rangePoint.controlPointList();
            }
        }
    }
    return controlPointList;
}

QList<ControlPointStruct> RangeListXml::controlPointList(const QList<ControlPointStruct> &sourcePointList)
{
    QList<ControlPointStruct> controlPointList;
    foreach (const ControlPointStruct &sourcePoint, sourcePointList)
    {
        controlPointList.append(this->controlPointList(sourcePoint));
    }
    return controlPointList;
}


